<template>
  <view>
    <quick-button></quick-button>
    <home v-if="tabbarValue == 0"></home>
    <order-list v-if="tabbarValue == 1" ref="myOrderList"></order-list>
    <order-list-two ref="myOrderListTwo" v-if="tabbarValue == 1 && roles == 2"></order-list-two>
    <my ref="user" v-if="tabbarValue == 2" @tabChange="onTabbarChange"></my>
    <TabBar :tabbarValue="tabbarValue" @onTabBarChange="onTabbarChange"></TabBar>
  </view>
</template>

<script>
import { toast, showConfirm, tansParams } from "@/utils/common";
import TabBar from "../../components/tab-bar.vue";

import orderList from "../orderList/orderList.vue";
import orderListTwo from "../orderList/orderListTwo.vue";
import home from "../home/home.vue";
import my from "../my/my.vue";
import * as wxPay from "../../common/api/wxPay.js";
// import * as orderInfo from '../../common/api/orderInfo.js'
export default {
  components: {
    TabBar,
    home,
    orderList,
    orderListTwo,
    my,

  },
  data() {
    return {
      tabbarValue: 0,
      titles: ["首页", "订单"],
      bannerList: ["https://bedfamily.eos.xian-2.cmecloud.cn/profile/xiaochengxu/static/banner.png"], //轮播图片
      roles: "",
    };
  },
  onHide() {
    console.log("隐藏");
    uni.removeStorageSync("nav_to_nurse_index");
  },
  onShow() {
    uni.removeStorageSync("nav_to_nurse_index");
    this.roles = uni.getStorageSync("ROLES");
  },
  onLoad(opt) {
    uni.removeStorageSync("selectedCategory")
    uni.removeStorageSync("selectedService")
    uni.removeStorageSync("selectedServices")
    uni.removeStorageSync("old_selectedCategory")
    uni.removeStorageSync("old_selectedService")
    this.roles = uni.getStorageSync("ROLES");
    let index = uni.getStorageSync("nav_to_nurse_index");
    if (index) {
      this.onTabbarChange(index);
    } else {
      this.onTabbarChange(0);
    }
    if (opt.type) {
      this.tabbarValue = opt.type;
    }
  },
  onPullDownRefresh() {
    console.log("onPullDownRefresh", onPullDownRefresh);

    this.refresh();
  },
  onReachBottom() {
    this.loadMore();
  },
  methods: {
    refreshUser() {
      this.$refs.user.getUserInfo();
    },
    onTabbarChange(e) {
      this.tabbarValue = e;
      uni.setNavigationBarTitle({
        title: this.titles[e],
      });
      uni.setStorageSync("nav_to_nurse_index", e);
      this.refresh();
    },
    onClickDing() {
      let templeteId = "1p7Z_SSvpsYroEhNz9OKedmie1bnUwzkx3XvgYD2mDg";
      // let templeteId = [
      //   "1p7Z_SSvpsYroEhNz9OKedmie1bnUwzkx3XvgYD2mDg",
      // ];
      uni.getSetting({
        withSubscriptions: true,
        success(res) {
          console.log(res, "订阅信息", res.subscriptionsSetting);
          if (!res.subscriptionsSetting.mainSwitch) {
            uni.openSetting({
              success(res) {
                console.log("打开设置页", res.authSetting);
              },
            });
          } else {
            uni.requestSubscribeMessage({
              tmplIds: [templeteId],
              provider: "weixin",
              success(res) {
                console.log(
                  "requestSubscribeMessage 订阅信息",
                  res,
                  res[templeteId]
                );

                if (res[templeteId] == "accept") {
                  // 用户点击确定后
                  console.log("用户订阅点击确定按钮");
                  // 后端接口
                } else {
                  console.log("拒绝，不会再弹出弹框 只能去设置页膝盖");
                  uni.showModal({
                    title: "您未开启消息订阅",
                    content: "为了给您提供更好的服务，请您授权消息订阅",
                    success: (res2) => {
                      if (res2.confirm) {
                        uni.openSetting({
                          success(res) {
                            console.log("打开设置页", res.authSetting);
                          },
                        });
                      } else {
                        console.log("决绝");
                      }
                    },
                  });
                }
              },
              fail(errMessage) {
                console.log("订阅消息 失败 ", errMessage);
              },
              complete(errMessage) {
                console.log("成功 失败都执行 ", errMessage);
              },
            });
          }
        },
      });
    },
    refresh() {
      const index = {
        0: () => {
          uni.stopPullDownRefresh();
        },
        1: () => {
          this.$refs.myOrderList.refresh();
        },
        2: () => {
          this.$refs.myOrderList.refresh();
        },
        3: () => {
          uni.stopPullDownRefresh();
        },
      };
      index[this.tabbarValue]();
    },
    loadMore() {
      const index = {
        0: () => { },
        1: () => {
          if (this.roles == 1) {
            // this.$refs.myOrderList.loadMore()
          } else {
            // this.$refs.myOrderListTwo.loadMore()
          }
        },
        2: () => { },
        3: () => { },
      };
      index[this.tabbarValue]();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep.u-tabbar--fixed {
  height: 60px;
  display: flex;
  justify-content: center;
}

.pay-btn {
  width: 228rpx;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0bc5ae;
  color: white;
  border-radius: 48rpx;
  font-size: 30rpx;
}
</style>
